package edu.thu.thss.yxy.gui;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Properties;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;

public class ConfigurationFrame extends JFrame {
	private JPanel labelPanel;
	private JLabel propsAreOptionalLabel;

	private JTable propertiesTable;
	private JScrollPane propertiesPane;
	private PropertiesTableModel properties;

	private JPanel buttonsPane;
	private JButton cancelButton;
	private JButton saveButton;

	public ConfigurationFrame() {
		initComponents();
		propertiesTable.setModel(properties);
        pack();
	}

	private void initComponents() {
		labelPanel = new JPanel();
		propsAreOptionalLabel = new JLabel();

		propertiesTable = new JTable();
		propertiesPane = new JScrollPane();
		properties = new PropertiesTableModel();

		buttonsPane = new JPanel();
		cancelButton = new JButton();
		saveButton = new JButton();
		
		Container contentPane = getContentPane();
		contentPane.setLayout(new BorderLayout());
		
		setResizable(false);
        setTitle("GoPhone - Configure");
		
		propsAreOptionalLabel.setForeground(UIManager.getColor("OptionPane.questionDialog.border.background"));
	    propsAreOptionalLabel.setText("All properties are OPTIONAL!");
	    labelPanel.setBorder(BorderFactory.createEmptyBorder(3,0,3,0));
	    labelPanel.add(propsAreOptionalLabel);
	    contentPane.add(labelPanel, BorderLayout.NORTH);
	    
	    propertiesPane.setViewportView(propertiesTable);
	    propertiesPane.setMinimumSize(new Dimension(503, 403));
	    propertiesPane.setPreferredSize(new Dimension(503, 403));
	    contentPane.add(propertiesPane,  BorderLayout.CENTER);
	    
		saveButton.setMnemonic('S');
        saveButton.setText("Save");
        cancelButton.setMnemonic('C');
        cancelButton.setText("Cancel");
        cancelButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				ConfigurationFrame.this.dispose();
			}
        });
        buttonsPane.add(saveButton);
        buttonsPane.add(cancelButton);
        contentPane.add(buttonsPane, BorderLayout.SOUTH);
	}

	public JButton getSaveButton() {
		return saveButton;
	}

	public JTable getPropertiesTable() {
		return propertiesTable;
	}
	
	public void setProperties(Properties properties){
		this.properties.setProperties(properties);
	}
}
